package com.boot.ys.mybatisplus.controller;


import com.alibaba.excel.EasyExcel;
import com.boot.ys.mybatisplus.entity.Easyexcel;
import com.boot.ys.mybatisplus.entity.vo.OrderExcelExportVO;
import com.boot.ys.mybatisplus.listener.EasyExcelListener;
import com.boot.ys.mybatisplus.service.EasyexcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Yang Sheng
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/easyexcel")
@Api(tags = "EasyExcel导入导出")
public class EasyexcelController {

    @Autowired
    private EasyexcelService easyexcelService;

    public static void main(String[] args) {

        // 指定文件夹路径
        String folderPath = "E:\\BaiduNetdiskDownload\\1111";
        // 指定新的文件夹名

        // 创建File对象，表示要重命名的目录
        File folder = new File(folderPath);

        if (folder.isDirectory()) {
            // 获取目录下所有文件夹
            File[] subFolders = folder.listFiles(File::isDirectory);

            // 遍历文件夹列表
            for (File subFolder : subFolders) {
                // 获取文件夹名
                String folderName = subFolder.getName();

                // 构造新的文件夹名
                String newFolderName = folderName + "-VX:alashengboy";

                // 创建新的File对象，表示重命名后的文件夹
                File newFolder = new File(subFolder.getParent() + File.separator + newFolderName);

                // 重命名文件夹
                if (subFolder.renameTo(newFolder)) {
                    System.out.printf("文件夹 %s 重命名为 %s%n", folderName, newFolderName);
                } else {
                    System.out.printf("文件夹 %s 重命名失败%n", folderName);
                }
            }
        }
    }

    /**
     * 这里这段逻辑是写的合并部分单元和的语句
     */
    @ApiOperation("导出EasyExcel")
    @GetMapping("hebingExplort")
    public void hebingExplort() {

        OrderExcelExportVO orderVO1 = new OrderExcelExportVO();
        orderVO1.setOrderId("1");
        orderVO1.setCommodityName("香蕉");
        orderVO1.setCommodityPrice("5");
        orderVO1.setPlateMoney("10");

        OrderExcelExportVO orderVO2 = new OrderExcelExportVO();
        orderVO2.setOrderId("1");
        orderVO2.setCommodityName("菠萝");
        orderVO2.setCommodityPrice("2");
        orderVO2.setPlateMoney("10");

        OrderExcelExportVO orderVO3 = new OrderExcelExportVO();
        orderVO3.setOrderId("2");
        orderVO3.setCommodityName("山楂");
        orderVO3.setCommodityPrice("1");
        orderVO3.setPlateMoney("1");


        ArrayList<OrderExcelExportVO> list = new ArrayList<>();
        list.add(orderVO1);
        list.add(orderVO2);
        list.add(orderVO3);

        System.out.println(list);

    }


    /**
     * 整合阿里的EasyExcel
     */
    //导出
    @ApiOperation("导出EasyExcel")
    @GetMapping("/exportEasyExcel")
    public void exportEasyExcel(HttpServletResponse response) {

        easyexcelService.exportEasyExcel(response);
    }


    // easyexcel上传文件
    @ApiOperation("导入Excel")
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Easyexcel.class, new EasyExcelListener(easyexcelService)).sheet().doRead();
        return "上传成功";
    }


}
